package cn.cmeizu.spring6.resource;

import org.springframework.core.io.UrlResource;

/**
 * @author cmeizu
 * @date 2023/2/19 13:49
 * 演示UrlResource访问网络资源
 */
public class UrlResourceDemo {
  public static void main(String[] args) {
    //http前缀开头的
    String httpPath = "http://www.baidu.com";
    loadUrlResource(httpPath);
    System.out.println("======================");
    //file前缀
    String filePath = "file:cmeizu.txt";
    loadUrlResource(filePath);

  }

  /**
   * 访问前缀是http,file
   */
  public static void loadUrlResource(String path) {
    //创建Resource实现类的对象 UrlResource
    try {
      UrlResource url = new UrlResource(path);
      //获取资源信息
      System.out.println("url.getFilename() = " + url.getFilename());
      System.out.println("url.getURI() = " + url.getURI());
      System.out.println("url.getDescription() = " + url.getDescription());
      System.out.println("url.getInputStream().read() = " + url.getInputStream().read());
    } catch (Exception e) {
      e.printStackTrace();
    }
    //
  }
}
